import axios, {
    type AxiosInstance,
    type AxiosRequestConfig,
    type AxiosResponse,
    type InternalAxiosRequestConfig
} from "axios";
import { getBaseURL, getToken } from "@/utils";
import type { ResponseType } from "@/types/httpType.ts";
import { Message } from "@arco-design/web-vue";
import router from "@/router";
import Local from "@/utils/local";

class HttpRequest {

    private instance: AxiosInstance

    constructor(config: AxiosRequestConfig) {
        this.instance = axios.create(config)
        this.interceptors()
    }

    private interceptors() {
        // 请求拦截器
        this.instance.interceptors.request.use(
            (config: InternalAxiosRequestConfig) => {
                const token = getToken()
                if (token) {
                    config.headers["token"] = token
                }
                // 添加缓存控制头，防止浏览器缓存
                config.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
                config.headers["Pragma"] = "no-cache"
                config.headers["Expires"] = "0"
                return config
            },
            (error) => {
                return Promise.reject(error)
            }
        )
        // 响应拦截器
        this.instance.interceptors.response.use(
            (res: AxiosResponse) => {
                if (res.data.code >= 200) {
                    if (res.data.code !== 200) {
                        if (res.data.code === 1001) {
                            Local.localClear()
                            router.go(0)
                            // 如果为添加分配的接口 就不显示这个小的提示框
                        } else if (res.config.url !== 'project-distribute/add') {
                            console.log(res.config, "res.configv");
                            Message.warning(res.data.msg);
                        }
                    }
                    return res.data
                }
                if (res.config.responseType === 'blob') {
                    return res.data
                }
                return Promise.reject(res)
            },
            (err) => {

                return Promise.reject(err)
            }
        )
    }

    public get<T = any>(url: string, config?: AxiosRequestConfig): Promise<ResponseType<T>> {
        return this.instance.get(url, config)
    }

    public post<T = any>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<ResponseType<T>> {
        return this.instance.post(url, data, config)
    }

    public put<T = any>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<ResponseType<T>> {
        return this.instance.put(url, data, config)
    }

    public delete<T = any>(url: string, params?: unknown): Promise<ResponseType<T>> {
        return this.instance.delete(url, { params })
    }

    public upload(url: string, data: unknown) {
        return this.instance.request({
            url,
            method: 'post',
            data,
            headers: {
                'Content-Type': 'application/json'
            },
            responseType: 'blob'
        })
    }

    public getUpload(url: string, params?: any): Promise<Blob> {
        return this.instance.request({
            url,
            method: 'get',
            headers: {
                'Content-Type': 'application/json'
            },
            responseType: 'blob',
            params
        })
    }


    public getUpload2(url: string, params: unknown): Promise<Blob> {
        return this.instance.request({
            url,
            method: 'get',
            responseType: 'blob',
            params: params
        })
    }
}

const http = new HttpRequest({
    baseURL: getBaseURL(),
})
export default http